package com.tre.jdevtemplateboot.config;

import com.tre.jdevtemplateboot.common.dto.PropertiesConfig;
import com.tre.jdevtemplateboot.common.enums.AuthenticateTypeEnums;
import com.tre.jdevtemplateboot.common.enums.ResourceHandlerEnums;
import com.tre.jdevtemplateboot.web.interceptor.JDevRequestHeadJwtInterceptor;
import com.tre.jdevtemplateboot.web.interceptor.JDevRequestHeadRedisInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author JDev
 */
@Configuration
public class WebMvcAppConfig implements WebMvcConfigurer {

    @Autowired
    private JDevRequestHeadRedisInterceptor jDevRequestHeadRedisInterceptor;
    @Autowired
    private JDevRequestHeadJwtInterceptor jDevRequestHeadJwtInterceptor;
    @Autowired
    private PropertiesConfig propertiesConfig;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        List<String> addPath = new ArrayList<>();
        List<String> excludePath = new ArrayList<>();
        String[] urls;
        if (StringUtils.hasLength(propertiesConfig.getAddPathPatterns())) {
            urls = propertiesConfig.getAddPathPatterns().split(",");
            for (String url : urls) {
                addPath.add(url);
            }
        }

        if (StringUtils.hasLength(propertiesConfig.getExcludePathPatterns())) {
            urls = propertiesConfig.getExcludePathPatterns().split(",");
            for (String url : urls) {
                excludePath.add(url);
            }
        }

        if (AuthenticateTypeEnums.JWT.toString().equalsIgnoreCase(propertiesConfig.getTokenCategory())) {
            registry.addInterceptor(jDevRequestHeadJwtInterceptor)
                    .addPathPatterns(addPath)
                    .excludePathPatterns(excludePath);
        } else if (AuthenticateTypeEnums.REDIS.toString().equalsIgnoreCase(propertiesConfig.getTokenCategory())) {
            registry.addInterceptor(jDevRequestHeadRedisInterceptor)
                    .addPathPatterns(addPath)
                    .excludePathPatterns(excludePath);
        }
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        if (!registry.hasMappingForPattern(ResourceHandlerEnums.RESOURCE_MSG_UPLOAD.getHandler())) {
            registry.addResourceHandler(ResourceHandlerEnums.RESOURCE_MSG_UPLOAD.getHandler())
                    .addResourceLocations(ResourceHandlerEnums.RESOURCE_MSG_UPLOAD.getLocation());
        }
        if (!registry.hasMappingForPattern(ResourceHandlerEnums.RESOURCE_MSG_STATIC.getHandler())) {
            registry.addResourceHandler(ResourceHandlerEnums.RESOURCE_MSG_STATIC.getHandler())
                    .addResourceLocations(ResourceHandlerEnums.RESOURCE_MSG_STATIC.getLocation());
        }

        if (!registry.hasMappingForPattern(ResourceHandlerEnums.RESOURCE_MSG_SWAGGER.getHandler())) {
            registry.addResourceHandler(ResourceHandlerEnums.RESOURCE_MSG_SWAGGER.getHandler())
                    .addResourceLocations(ResourceHandlerEnums.RESOURCE_MSG_SWAGGER.getLocation());
        }

        if (!registry.hasMappingForPattern(ResourceHandlerEnums.RESOURCE_MSG_DOC.getHandler())) {
            registry.addResourceHandler(ResourceHandlerEnums.RESOURCE_MSG_DOC.getHandler())
                    .addResourceLocations(ResourceHandlerEnums.RESOURCE_MSG_DOC.getLocation());
        }

        if (!registry.hasMappingForPattern(ResourceHandlerEnums.RESOURCE_MSG_WEB_JARS.getHandler())) {
            registry.addResourceHandler(ResourceHandlerEnums.RESOURCE_MSG_WEB_JARS.getHandler())
                    .addResourceLocations(ResourceHandlerEnums.RESOURCE_MSG_WEB_JARS.getLocation());
        }

        if (!registry.hasMappingForPattern(ResourceHandlerEnums.RESOURCE_MSG_FAVICON.getHandler())) {
            registry.addResourceHandler(ResourceHandlerEnums.RESOURCE_MSG_FAVICON.getHandler());
        }
    }

}
